<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\inspection\controller\api;
use app\common\controller\Api;
use think\response\Json;

/**
 * 巡检任务管理
 * @package app\Mission
 * @author LJQ<245369209@qq.com>
 */
class Mission extends Api

{

    /**
   *  巡检任务列表
   * @param string  string charge 巡检员ID,如果有只显示巡检员自己的巡检任务列表.
   * @return Json
   */

	public function missionList($charge = null){
            $where = [] ;
            if($charge){
      
               $where['chargePerson'] = ['eq',$charge];
             }
		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			// 巡检任务列表
			$info =collection(model('Mission')->with('user,enterprise,items')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			// 巡检任务列表
			$info =collection(model('Mission')->with('user,enterprise,items')->where($where)->select())->toArray();
		}
	   	 
	 	//总数
	   	 $count = model('Mission')->where($where)->count('id'); 
		  
   		if (empty($info) && $count == 0) {
         	return json(['code'=>1,'msg'=>'没有找到巡检任务','count'=>$count,'data'=>$info]);
       	} 
       	
       	if ($count > 0){
       		return json(['code'=>1,'msg'=>'巡检巡检任务','count'=>$count,'data'=>$info]);
       	}
            return json(['code'=>0,'msg'=>'数据异常']);
   	}
   	  
   	      //添加巡检任务
   	 public function add($data){
          
          if (empty($data)) {
              return json(['code'=>0,'msg'=>'数据异常,请检查']);
          }
          $model = model('Mission');
          $result = $model->change($data);
          if (false != $result) {
              setlog('Missionadd','Mission',session('user_auth.id'));

              return json(['code'=>1,'msg'=>'巡检任务添加成功']);
          } else {
              return json(['code'=>0,'msg'=>'巡检任务添加失败,请检查']);
          }
      }

   	

   /**
   * 巡检任务修改
   * @param  string MissionId 巡检任务id,
   * @return Json
   */
   	public function edit($data,$missionId){
        
          if (empty($missionId)) {
              return json(['code'=>0,'msg'=>'巡检任务不存在,请检查']);
          }
          $model = model('Mission');     
          if ($model->change($data)) {
             setlog('Missionedit','Mission',session('user_auth.id'));
             return json(['code'=>1,'msg'=>'巡检任务信息修改成功']);
          }else{
             return json(['code'=>0,'msg'=>'巡检任务信息修改失败,请检查']);
          }
    }
    
    /**
     * 巡检任务配置
     * @param array $data
     * @param string $missionId
     * return Json
     */
    public function config($data,$missionId){
    	$model = model('Mission');
    	$mission = $model->find($missionId);
    	if(!$mission){
    		return json(['code'=>0,'msg'=>'巡检任务不存在,请检查']);
    	}
    	$itemIds = [];
    	foreach($mission['items'] as $value){
    		$itemIds[] = $value['id'];
    	}
    	$mission->items()->detach($itemIds);
    	if(isset($data['items'])){
	    	if($mission->items()->saveAll($data['items'])){
          setlog('Missionupdate','Mission',session('user_auth.id'));
	    		return json(['code'=>1,'msg'=>'巡检任务配置成功']);
	    	}else{
	    		return json(['code'=>0,'msg'=>'巡检任务配置失败,请检查']);
	    	}
    	}
    	return json(['code'=>1,'msg'=>'巡检任务配置成功']);
    	
    }
    /**
   *  删除巡检任务
   * @param string MissionId 巡检任务id,
   * @param Json
   */
     
    public function del($missionId) {
        $model = model('Mission');
      	if (!$mission = $model->find($missionId)) {
           	return  json(['code'=>0,'msg'=>'巡检任务id不存在,请检查']);
        }
        $items = [];
        foreach($mission->items as $value){
        	$items[] = $value['id'];
        }
        
        if($mission->items()->detach($items) !== FALSE &&$model->destroy(['id'=>$missionId]) !== FALSE){
           setlog('Missiondel','Mission',session('user_auth.id'));
        	 return json(['code'=>1,'msg'=>'巡检任务删除成功']);
       }else{
         	 return json(['code'=>0,'msg'=>'巡检任务删除失败,请检查']);
       }
    }
    
    /**
     * 解绑巡检任务
     * @param string $missionId
     * @param string $itemId
     * @return Json
     */
    public function detach($missionId,$itemId){
    	$model = model('Mission');
    	if (!$mission = $model->find($missionId)) {
    		return  json(['code'=>0,'msg'=>'巡检任务id不存在,请检查']);
    	}
    	if($mission->items()->detach($itemId)!== FALSE){
          setlog('Missiondel','Mission',session('user_auth.id'));
    		return json(['code'=>1,'msg'=>'巡检项目解绑成功']);
    	}else{
         	 return json(['code'=>0,'msg'=>'巡检任务解绑失败,请检查']);
       }
    }

     
   /**
   *   查询巡检任务
   * @param keyword巡检任务名称
   * @param area区域ID
   * @return Json
   */
   	public function search($keyword = null,$enterprise = null){
            $where = [] ;
   
           
		if($keyword){
			
			$where['patrolPoint'] = ['like','%'.$keyword.'%'];
		}

            if ($enterprise) {
                   $where['enterpriseId'] = ['eq',$enterprise];
            }
   		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			//巡检巡检任务
			$info =collection(model('Mission')->with('user,enterprise,items')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//巡检巡检任务
			$info =collection(model('Mission')->with('user,enterprise,items')->where($where)->select())->toArray();
		}
	 	//总数
	       $count = model('Mission')->where($where)->count('id'); 
		 
    	if ($count == 0) {
            return json(['code'=>1,'msg'=>'没有找到巡检任务','count'=>$count,'data'=>$info]);
       	} 
       	
       	if ($count > 0){
       		return json(['code'=>1,'msg'=>'巡检任务','count'=>$count,'data'=>$info]);
       	}
      }

}